﻿using System;
using System.IO;
using System.Windows.Forms;
using System.Xml.Serialization;

namespace QuickEmailer
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void btnGetInputFile_Click(object sender, EventArgs e)
        {
            ofdInputFile.ShowDialog();
            txtInputFile.Text = ofdInputFile.FileName;
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            txtSmtpUser.Enabled = !(cbSmtpAnonymous.Checked);
            txtSmtpPass.Enabled = !(cbSmtpAnonymous.Checked);

            CheckRequiredFields(sender, e);
        }

        private void btnSendMail_Click(object sender, EventArgs e)
        {
            DialogResult sendit = MessageBox.Show("Ready to send email?", "Continue?", MessageBoxButtons.YesNo,
                                                  MessageBoxIcon.Question);
            if (sendit == DialogResult.Yes)
            {
                frmSendingDialog frmProcess = new frmSendingDialog();

                frmProcess.InputFile = txtInputFile.Text;

                frmProcess.SmtpHost = txtSmtpServer.Text;
                frmProcess.SmtpAnonymous = cbSmtpAnonymous.Checked;
                frmProcess.SmtpUser = txtSmtpUser.Text;
                frmProcess.SmtpPass = txtSmtpPass.Text;
                frmProcess.SmtpPort = int.Parse(txtSmtpPort.Text);
                frmProcess.Subject = txtSubject.Text;
                frmProcess.EmailMessage = txtMessage.Text;
                frmProcess.EmailHtml = cbSendAsHtml.Checked;
                frmProcess.FromName = txtFromName.Text;
                frmProcess.FromEmail = txtFromEmail.Text;

                frmProcess.ShowDialog();
            }

        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void onlineHelpToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Help.ShowHelp(this, "http://quickemailer.codeplex.com/documentation");
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmAbout about = new frmAbout();
            about.ShowDialog();
        }

        private void saveSettingsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            EmailerSettings settings = new EmailerSettings();
            settings.EmailHtml = cbSendAsHtml.Checked;
            settings.FromEmail = txtFromEmail.Text;
            settings.FromName = txtFromName.Text;
            settings.SmtpAnonymous = cbSmtpAnonymous.Checked;
            settings.SmtpUser = txtSmtpUser.Text;
            settings.SmtpPass = txtSmtpPass.Text;
            settings.SmtpPort = int.Parse(txtSmtpPort.Text);
            settings.SmtpServer = txtSmtpServer.Text;

            string fileSave = Application.StartupPath + @"\settings.xml";
            
            XmlSerializer serializer = new XmlSerializer(typeof(EmailerSettings));

            try
            {
                StreamWriter writer = new StreamWriter(fileSave);
                serializer.Serialize(writer, settings);

                if (File.Exists(fileSave))
                {
                    MessageBox.Show("Settings have been saved to " + fileSave + "!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            //need to do better exception handling here.
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            
        }

        private void CheckRequiredFields(object sender, EventArgs e)
        {
            btnSendMail.Enabled = ((txtInputFile.Text != "") &&
                                   (txtSmtpServer.Text != "") &&
                                   (txtFromName.Text != "") &&
                                   (txtFromEmail.Text != "") &&
                                   (txtSubject.Text != "") &&
                                   (txtMessage.Text != "") &&
                                   ((cbSmtpAnonymous.Checked) || ((txtSmtpUser.Text != "") && (txtSmtpPass.Text != "")))
                                  );

        }

    }
}
